Imports System.IO
Imports System.Data.SqlClient
Imports System
Imports System.Text
Imports System.Data.Odbc
Public Class FrmInvestigation
    Public GV_ISEDIT As Boolean = False
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Panel2.Enabled = False
        Close()
        FrmDiagnosis.Close()
        FrmDiagnosis.Show()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        GV_ISEDIT = False

        Panel2.Enabled = True
        btnEdit.Visible = False
        btnDel.Visible = False
        btnAdd.Visible = False
        btnSave.Visible = True
        btnCancel.Visible = True
        ClearText()
    End Sub
    Private Sub ClearText()
        cbxCXR.SelectedIndex = -1
        cbxCT.SelectedIndex = -1
        cbxBone.SelectedIndex = -1
        RichTxtNotes.Text = ""
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        If GV_SELECTED_INVESTID <> "" Then
            GV_ISEDIT = True
            Panel2.Enabled = True
            btnEdit.Visible = False
            btnDel.Visible = False
            btnAdd.Visible = False
            btnSave.Visible = True
            btnCancel.Visible = True
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        SaveInvestigation()
        Panel2.Enabled = False
        Close()

        If GV_SELECTED_DATAENTRY_MODE = "PRESENTATION" Then
            FrmPresentDataEntry.Close()
            FrmPresentDataEntry.Show()
            FrmPresentDataEntry.Focus()

        Else
            FrmDiagnosis.Close()
            FrmDiagnosis.Show()
            FrmDiagnosis.Focus()
        End If


    End Sub
    Private Sub SaveInvestigation()
        Dim myCommand As SqlCommand
        Dim SqlConn As New SqlConnection()
        SqlConn = New SqlConnection(GV_CONNETION)
        myCommand = New SqlCommand("spInsertInvestigation", SqlConn)
        myCommand.CommandType = CommandType.StoredProcedure
        SqlConn.Open()
        Try
            myCommand.Parameters.Add("@Inv_ID", SqlDbType.VarChar)
            myCommand.Parameters.Add("@Event_ID", SqlDbType.VarChar)
            myCommand.Parameters.Add("@Pt_id1", SqlDbType.VarChar)
            myCommand.Parameters.Add("@CXR", SqlDbType.VarChar)
            myCommand.Parameters.Add("@CT", SqlDbType.VarChar)
            myCommand.Parameters.Add("@BoneScan", SqlDbType.VarChar)
            myCommand.Parameters.Add("@Notes", SqlDbType.VarChar)
            myCommand.Parameters.Add("@EnteredBy", SqlDbType.VarChar)
            myCommand.Parameters("@Inv_ID").Value = GV_SELECTED_INVESTID
            myCommand.Parameters("@Pt_id1").Value = GV_CUR_PATID
            myCommand.Parameters("@Event_ID").Value = GV_SELECTED_EVENTID
            myCommand.Parameters("@CXR").Value = Trim(cbxCXR.SelectedValue)
            myCommand.Parameters("@CT").Value = Trim(cbxCT.SelectedValue)
            myCommand.Parameters("@BoneScan").Value = Trim(cbxBone.SelectedValue)
            myCommand.Parameters("@Notes").Value = Trim(RichTxtNotes.Text)
            myCommand.Parameters("@EnteredBy").Value = GV_USER


            myCommand.ExecuteNonQuery()

            MsgBox("INVESTID " + GV_SELECTED_INVESTID + " , " + "Event_ID " + GV_SELECTED_EVENTID + " ")
        Catch ex As Exception
            Console.WriteLine(ex.StackTrace)
        End Try


        myCommand.Cancel()
        SqlConn.Close()
        myCommand = Nothing
        SqlConn = Nothing
    End Sub

    Private Sub FrmInvestigation_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Panel2.Enabled = False
        LoadCXR()
        LoadCT()
        LoadBone()
        If GV_SELECTED_INVESTID <> "" Then
            ListInvestigation(GV_SELECTED_INVESTID)
            If GV_FRMEVENT_ALLOW_EDIT = True Then btnEdit_Click(sender, e)
        End If

        Panel1.Enabled = GV_FRMEVENT_ALLOW_EDIT

        If Approved() = True Then
            'btnAdd.Enabled = False
            'btnEdit.Enabled = False
            'btnDel.Enabled = False
            'Panel1.Enabled = False
            'Panel2.Enabled = False
            btnEdit.Enabled = False
            btnSave.Enabled = False
            BtnLock.Text = "Unlock"
            btnDel.Enabled = False
        End If
    End Sub
    Function Approved() As Boolean
        Dim vStatus As String = ""
        Dim vLockBy As String = ""
        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("select * from Investigation where Inv_ID = '" & GV_SELECTED_INVESTID & "'", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Do While objReader.Read
                vStatus = IIf(IsDBNull(objReader("Status")), "", objReader("Status"))
                vLockBy = IIf(IsDBNull(objReader("LockedBy")), "", objReader("LockedBy"))
            Loop

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
        If vLockBy <> "" Then
            Approved = True
        Else
            Approved = False
        End If
    End Function
    Private Sub LoadBone()

        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("exec spGetlookup_Value  8", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Dim i As Integer
            i = 1
            Dim list As New DataTable
            list.Columns.Add(New DataColumn("code_desc", GetType(String)))
            list.Columns.Add(New DataColumn("code", GetType(String)))
            list.Rows.Add(list.NewRow())
            list.Rows(0)(0) = ""
            list.Rows(0)(1) = ""
            Do While objReader.Read
                list.Rows.Add(list.NewRow())
                list.Rows(i)(0) = IIf(IsDBNull(objReader("code_desc")), "", objReader("code_desc"))
                list.Rows(i)(1) = IIf(IsDBNull(objReader("code")), "", objReader("code"))
                i = i + 1
            Loop
            Me.cbxBone.DataSource = list
            Me.cbxBone.DisplayMember = "code_desc"
            Me.cbxBone.ValueMember = "code"

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
    End Sub
    Private Sub LoadCT()


        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("exec spGetlookup_Value  6", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Dim i As Integer
            i = 1
            Dim list As New DataTable
            list.Columns.Add(New DataColumn("code_desc", GetType(String)))
            list.Columns.Add(New DataColumn("code", GetType(String)))
            list.Rows.Add(list.NewRow())
            list.Rows(0)(0) = ""
            list.Rows(0)(1) = ""
            Do While objReader.Read
                list.Rows.Add(list.NewRow())
                list.Rows(i)(0) = IIf(IsDBNull(objReader("code_desc")), "", objReader("code_desc"))
                list.Rows(i)(1) = IIf(IsDBNull(objReader("code")), "", objReader("code"))
                i = i + 1
            Loop
            Me.cbxCT.DataSource = list
            Me.cbxCT.DisplayMember = "code_desc"
            Me.cbxCT.ValueMember = "code"

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
    End Sub
    Private Sub LoadCXR()


        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("exec spGetlookup_Value  5", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Dim i As Integer
            i = 1
            Dim list As New DataTable
            list.Columns.Add(New DataColumn("Code_desc", GetType(String)))
            list.Columns.Add(New DataColumn("Code", GetType(String)))
            list.Rows.Add(list.NewRow())
            list.Rows(0)(0) = ""
            list.Rows(0)(1) = ""
            Do While objReader.Read
                list.Rows.Add(list.NewRow())
                list.Rows(i)(0) = IIf(IsDBNull(objReader("Code_desc")), "", objReader("Code_desc"))
                list.Rows(i)(1) = IIf(IsDBNull(objReader("Code")), "", objReader("Code"))
                i = i + 1
            Loop
            Me.cbxCXR.DataSource = list
            Me.cbxCXR.DisplayMember = "Code_desc"
            Me.cbxCXR.ValueMember = "Code"

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
    End Sub
    Private Sub ListInvestigation(ByVal vInvestID)
        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("select * from Investigation where Inv_ID = '" & vInvestID & "'", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Do While objReader.Read
                cbxCXR.SelectedValue = IIf(IsDBNull(objReader("CXR")), "", objReader("CXR"))
                cbxCT.SelectedValue = IIf(IsDBNull(objReader("CT")), "", objReader("CT"))
                cbxBone.SelectedValue = IIf(IsDBNull(objReader("BoneScan")), "", objReader("BoneScan"))
                RichTxtNotes.Text = IIf(IsDBNull(objReader("Notes")), "", objReader("Notes"))
                Lbl_EnBy.Text = "Entered By:" & vbCrLf & IIf(IsDBNull(objReader("EnteredBy")), "", objReader("EnteredBy"))
                Lbl_UpdBy.Text = "Updated By:" & vbCrLf & IIf(IsDBNull(objReader("UpdatedBy")), "", objReader("UpdatedBy"))
                Lbl_LockBy.Text = "Locked By:" & vbCrLf & IIf(IsDBNull(objReader("LockedBy")), "", objReader("LockedBy"))

            Loop

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
    End Sub

    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
        If GV_SELECTED_INVESTID <> "" Then
            Dim response As Integer

            response = MsgBox("Are you sure want to delete this record?", MsgBoxStyle.YesNo, "Delete")
            If response = 7 Then
                Exit Sub
            End If

            Dim myCommand As SqlCommand
            Dim SqlConn As New SqlConnection()
            SqlConn = New SqlConnection(GV_CONNETION)
            myCommand = New SqlCommand("spDelInvestigation", SqlConn)
            myCommand.CommandType = CommandType.StoredProcedure
            SqlConn.Open()

            myCommand.Parameters.Add("@inv_ID", SqlDbType.VarChar)
            myCommand.Parameters.Add("@EnteredBy", SqlDbType.VarChar)
            myCommand.Parameters("@inv_ID").Value = GV_SELECTED_INVESTID
            myCommand.Parameters("@EnteredBy").Value = GV_USER
            myCommand.ExecuteNonQuery()

            myCommand.Cancel()
            SqlConn.Close()
            myCommand = Nothing
            SqlConn = Nothing
            Panel2.Enabled = False
            Close()
            FrmDiagnosis.Close()
            FrmDiagnosis.Show()
        End If


    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        If GV_ISEDIT = False Then ClearText() Else ListInvestigation(GV_SELECTED_INVESTID)
        Panel2.Enabled = False
        btnAdd.Visible = True
        btnEdit.Visible = True
        btnDel.Visible = True
        btnSave.Visible = False
        btnCancel.Visible = False
    End Sub

    Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint

    End Sub

    Private Sub BtnLock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLock.Click
        If BtnLock.Text = "Lock" Then
            If GV_FRMEVENT_ALLOW_APPROVE = False Then
                MsgBox("Permission denied.", MsgBoxStyle.Information)
                Exit Sub
            End If
        End If

        If BtnLock.Text = "UnLock" Then
            If GV_FRMEVENT_ALLOW_APPROVE = False Then
                MsgBox("Permission denied.", MsgBoxStyle.Information)
                Exit Sub
            End If
        End If

        Dim myCommand As SqlCommand
        Dim SqlConn As New SqlConnection()
        SqlConn = New SqlConnection(GV_CONNETION)
        myCommand = New SqlCommand("sp_LockUnlockInvestigation", SqlConn)
        myCommand.CommandType = CommandType.StoredProcedure
        SqlConn.Open()
        myCommand.Parameters.Add("@Inv_ID", SqlDbType.VarChar)
        myCommand.Parameters.Add("@EnteredBy", SqlDbType.VarChar)
        myCommand.Parameters.Add("@LockFlag", SqlDbType.VarChar)

        myCommand.Parameters("@Inv_ID").Value = GV_SELECTED_INVESTID
        myCommand.Parameters("@EnteredBy").Value = GV_USER
        myCommand.Parameters("@LockFlag").Value = Trim(BtnLock.Text)
        myCommand.ExecuteNonQuery()

        myCommand.Cancel()
        SqlConn.Close()
        myCommand = Nothing
        SqlConn = Nothing

        If BtnLock.Text = "Lock" Then
            BtnLock.Text = "UnLock"
            btnEdit.Enabled = False
            btnSave.Enabled = False
            btnDel.Enabled = False
            Lbl_LockBy.Text = "Locked By:" & vbCrLf & GV_USERNAME
        Else
            BtnLock.Text = "Lock"
            btnEdit.Enabled = GV_FRMEVENT_ALLOW_EDIT
            btnSave.Enabled = GV_FRMEVENT_ALLOW_EDIT
            btnDel.Enabled = GV_FRMEVENT_ALLOW_EDIT
            Lbl_LockBy.Text = "Locked By:"
        End If
    End Sub
End Class